#include "Mychattool.h"

//void print_time(time_t timer ,struct tm *tblock);

int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_in s_addr;
    socklen_t len;
    unsigned int port;
    char buf[BUFLEN];

    time_t timer;//time_t就是long int 类型
    struct tm *tblock;

    /*建立socket*/
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("socket");
        exit(errno);
    }else {
        print_time(timer,&tblock);
        printf("socket create success!\n");
    }

    /*设置服务器ip*/
    bzero(&s_addr, sizeof(s_addr));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(SERVER_PORT);
    s_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    /*开始连接服务器*/
    if(connect(sockfd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr)) == -1){
        print_time(timer,&tblock);
        perror("connect");
        exit(errno);
    }else {
        //print_time(timer,&tblock);
        printf("conncet success!\n\n");
    }
    printf("|***************** conncet——server ***************|\n");
    while(1){
        /*接收消息*/
        bzero(buf,BUFLEN);
        len = recv(sockfd,buf,BUFLEN,0);
        if(len > 0){
            print_time(timer,&tblock);

            if(!strncasecmp(buf,"emo",3)){
                printf("clien:> ");
                emo_out(buf);
                printf("\n\n" );
            }
            else
                printf("server:>%s \n\n",buf);
            //printf("server:>%s\n\n",buf,len);
            if(!strncasecmp(buf,"y",1)){
                print_time(timer,&tblock);
                printf("服务器同意终止聊天！我们再会~\n\n");
                break;
            }
            //printf("服务器发来的消息是：%s,共有字节数是: %d\n",buf,len);
        }
        else{
            if(len < 0 ){
                printf("接受消息失败！\n\n");
            }

            else{
                print_time(timer,&tblock);
                printf("对方下线了，聊天终止！\n");
            }
            break;
        }
    send_r:
        /*发送消息*/
        bzero(buf,BUFLEN);
        print_time(timer,&tblock);
        printf("client:> ");
        /*fgets函数：从流中读取BUFLEN-1个字符*/
        fgets(buf,BUFLEN,stdin);
        printf("\n" );

        //strncasecmp()用来比较参数s1和s2字符串前n个字符，忽略大小写的差异,一样返回0。
        if(!strncasecmp(buf,"quit",4)){
            print_time(timer,&tblock);
            printf("client 请求终止聊天!\n\n");
            bzero(buf,BUFLEN);
            strcpy (buf,"客户端请求终止聊天：y->是；n->否?\n ");
        }
        /*如果输入的字符串只有"\n"，即回车，那么请重新输入*/
        if(!strncmp(buf,"\n",1)){
            print_time(timer,&tblock);
            printf("输入的字符只有回车，这个是不正确的！！！\n\n");
            goto send_r;
        }
        /*如果buf中含有'\n'，那么要用strlen(buf)-1，去掉'\n'*/
        if(strchr(buf,'\n'))
            len = send(sockfd,buf,strlen(buf)-1,0);

        else
            len = send(sockfd,buf,strlen(buf),0);
        if(len < 0){
            print_time(timer,&tblock);
            printf("消息发送失败!\n\n");
            break;
        }
    }
    /*关闭连接*/
    //close(newfd);
    close(sockfd);

    return 0;
}
